﻿using AutoMapper;
using MediatR;
using Traceability.Common.Domain;
using Traceability.Common.Dtos;
using Traceability.Common.Infrastructure.InterFaces;
using Traceability.Enum;

namespace Traceability.Bais.API.Appliction.Command.Permissions
{
    public class PermissionCreateHandler : IRequestHandler<PermissionCreateCommon, Result>
    {
        private readonly IBaseRepository<Permission> permissionRep;
        private readonly IMapper _mapper;
        public PermissionCreateHandler(IBaseRepository<Permission> _permissionRep,IMapper _mapper)
        {
            this.permissionRep = _permissionRep;
            this._mapper = _mapper;
        }

        public async Task<Result> Handle(PermissionCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.add };
            try
            {
                

                Permission permission = _mapper.Map<Permission>(request);
                permission.CreatedTime = DateTime.Now;
                var result1 = await permissionRep.AddAsync(permission);

                if (result1 > 0)
                {
                    result.Code = (int)CommonErrorCode.sucess;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {
                result.Msg = "添加失败";
                throw;
            }
            return result;


        }
    }
}
